//
//  ViewController.m
//  UI01_UIView
//
//  Created by dllo on 16/6/3.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"
#import "CustomView.h"


@interface ViewController ()

@property (nonatomic,retain)CustomView *rainView;

@end

@implementation ViewController

-(void)dealloc{
    [_rainView release];
    [super dealloc];
}


#pragma mark - UIViewController 生命周期

//如果要重写UIViewController的初始化方法的话,一般都重写这个方法.
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self){
        NSLog(@"%d, %s",__LINE__, __FUNCTION__);
    }
    return self;
}
//初始化
-(instancetype)init{
    self = [super init];
    if(self){
        NSLog(@"%d, %s",__LINE__,__FUNCTION__);
    }
    return self;
}
//加载视图
-(void)loadView{
    [super loadView];
    NSLog(@"%d ,%s",__LINE__,__FUNCTION__);
}


//视图加载完成,加载子视图. self.view是blueView的父视图
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%d, %s",__LINE__,__FUNCTION__);
    self.view.backgroundColor = [UIColor greenColor];
    
//    [self creatSubViews];
//    [self creatSubViewsYellow];
    [self addCustomViews];
    [self creatButtons];
    
    // Do any additional setup after loading the view, typically from a nib.
}
//视图将要显示
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"%d,%s",__LINE__,__FUNCTION__);
}
//视图显示完成
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSLog(@"%d,%s",__LINE__,__FUNCTION__);
}

#pragma mark - 知识点2 UIView

//创建自定义方法,写加载子代码的视图.
-(void)creatSubViews{

    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 200, 300)];
    //视图背景颜色默认为clearColor;
    blueView.backgroundColor = [UIColor blueColor];
    //将子视图添加到父视图上
    [self.view addSubview:blueView];
    
    //内存管理
    [blueView release];
    
    
    //练习:创建一个视图,距离屏幕左边50,距离屏幕上边400,宽50,高30,背景颜色为黄色,添加到self.view视图上.
    UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(50, 400, 50, 30)];
    yellowView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:yellowView];
    [yellowView release];
    
    
    
    
    //将blackView放到blueView上.
    
    UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 30, 50)];
    blackView.backgroundColor = [UIColor blackColor];
    [blueView addSubview:blackView];
    [blackView release];
    
    /** 
     * frame是以父视图为参照的坐标系统
     */
    
    
    //UIView常用API
    
    //隐藏视图,默认值为NO, 为YES时将视图隐藏.
//    blueView.hidden = YES;
    
    //改变视图的透明度,CGFloat类型,范围从0.0 到 1.0,默认值为1.0. 1.0是完全不透明状态.
    blueView.alpha = 1.0;
    
    //获取视图的子视图
    NSLog(@"blueView subViews; %@",blueView.subviews);
    
    //获取视图的父视图(父视图只有唯一的一个).
    NSLog(@"blueView superView: %@",blueView.superview);
    
    //视图中心点坐标(对角线交叉的地方.)
    blueView.center =CGPointMake(100, 200);
    
    NSLog(@"x: %lf ,y: %lf ,w: %lf ,h: %lf",blueView.frame.origin.x,blueView.frame.origin.y,blueView.frame.size.width,blueView.frame.size.height);
    
    //视图的标记. tag
    blueView.tag = 100;
    
    //通过tag找到指定视图
    UIView *findView = [self.view viewWithTag:100];
    
    NSLog(@"blueView: %@",blueView);
    NSLog(@"findView: %@",findView);
    
    //输出一下self.view的子视图
    NSArray *svSubViewsArray = self.view.subviews;
    NSLog(@"self.view 的子视图: %@",svSubViewsArray);
    
    //创建一个视图,坐标为(50,100),宽高为(200,300),颜色为橙色,添加到self.view上.
    
    UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 300)];
    orangeView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:orangeView];
    [orangeView release];
    
    //创建一个视图,坐标为(100,150),宽高为(200,300),颜色为紫色,添加到self.View上.
    UIView *purpleView = [[UIView alloc] initWithFrame:CGRectMake(100, 150, 200, 300)];
    purpleView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:purpleView];
    [purpleView release];
    
    //把blueView 放到所有子视图的前面.
    [self.view bringSubviewToFront:blueView];
    
    //把purpleView 放到所有子视图的后面.
    [self.view sendSubviewToBack:purpleView];
    
    //创建视图,并将视图插入到下标出
    UIView *darkGrayView = [[UIView alloc] initWithFrame:CGRectMake(150, 200, 200, 300)];
    darkGrayView.backgroundColor = [UIColor darkGrayColor];
    [self.view insertSubview:darkGrayView atIndex:3];
    [darkGrayView release];
    
    [self.view sendSubviewToBack:darkGrayView];
    
    //创建视图,并将视图插入另一个子视图之前
    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(200, 250, 200, 300)];
    greenView.backgroundColor = [UIColor greenColor];
    [self.view insertSubview:greenView aboveSubview:purpleView];
    [greenView release];
    
    [self.view exchangeSubviewAtIndex:4 withSubviewAtIndex:0];
    
    //将子视图从父视图上移除.
    [blueView removeFromSuperview];
    
}

//创建自定义的视图
-(void)addCustomViews{
    
    CustomView *customView = [[CustomView alloc] initWithFrame:CGRectMake(50, 100, 100, 200)];
    customView.tag = 100;
    
    [self.view addSubview:customView];
    [customView release];
    
    CustomView *newCustomView = [[CustomView alloc ]initWithFrame:CGRectMake(100, 100, 100, 200) withColor:[UIColor yellowColor]];
    [self.view addSubview:newCustomView];
    [newCustomView release];
    
    
    
    self.rainView = [[CustomView alloc] initWithFrame:CGRectMake(200, 300, 100, 200) withColor:[UIColor blackColor]];
    self.rainView.tag = 99;
    [self.view addSubview:_rainView];
    [_rainView release];
    
    
}

#pragma mark - UIButton
-(void)creatButtons {
    //创建button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    //设置button颜色
    button.backgroundColor = [UIColor orangeColor];
    //确定在父视图上的位置
    button.frame = CGRectMake(100, 400, 50, 40);
    //添加到父视图上
    [self.view addSubview:button];
    
    //不需要对button进行release.
    
    //添加点击事件
    [button addTarget:self action:@selector(changeColorAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.backgroundColor = [UIColor blueColor];
    button2.frame = CGRectMake(100, 500, 50, 40);
    [self.view addSubview:button2];
    
    [button2 addTarget:self action:@selector(changeColorAction2:) forControlEvents:UIControlEventTouchUpInside];
    
}

//parameterButton 就是 button
-(void)changeColorAction:(UIButton *)parameterButton{
    
    //改变self.view的背景颜色.
    self.view.backgroundColor = [UIColor grayColor];
    
    //通过tag值找到指定的视图.
    CustomView *findView = (CustomView *)[self.view viewWithTag:100];
    findView.backgroundColor = [UIColor grayColor];
    
//    CustomView *findView2 = (CustomView *)[self.view viewWithTag:99];
//    findView2.backgroundColor =[UIColor redColor];
    
}

-(void)changeColorAction2:(UIButton *)parameterButton{
    self.rainView.backgroundColor = [UIColor redColor];
    
//    CustomView *findView = (CustomView *)[self.view viewWithTag:99];
//    findView.backgroundColor =[UIColor redColor];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
